package com.controller;


import com.pojo.Product;
import com.service.iter.IProductsService;
import com.service.iter.ISaleOrderService;
import com.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 产品控制器
 *
 * @author 蔡文欣
 * @date 2023/06/24 09:43:42
 */

@RestController
@RequestMapping("/products")
public class ProductsController {

    /**
     * 产品和服务
     */
    private final IProductsService productsService;
    private final ISaleOrderService saleOrderService;

    @Autowired
    public ProductsController(IProductsService productsService, ISaleOrderService saleOrderService) {
        this.productsService = productsService;
        this.saleOrderService = saleOrderService;
    }

    /**
     * 获取产品列表
     *
     * @param pageNo 页码
     * @param query  查询
     * @return {@link Res}
     */
    @GetMapping("productList/{pageNo}/{query}")
    public Res getProducts(@PathVariable Integer pageNo, @PathVariable String query) {
        Res res = new Res();
        List<Product> products = productsService.getProducts(pageNo, query);
        if (Objects.isNull(products)) {
            res.setCode(404);
            res.setMsg("获取产品列表失败");
        } else {
            res.setData(products);
            res.setCode(200);
        }
        return res;
    }

    /**
     * 获取产品
     *
     * @param productId 产品ID
     * @param sizeId    尺寸id。
     * @return {@link Res}
     */
    @GetMapping("/{productId}/{sizeId}")
    public Res getProduct(@PathVariable Long productId, @PathVariable Integer sizeId) {
        Res res = new Res();
        Product productPrice = productsService.getProductPrice(productId, sizeId);
        if (Objects.equals(null, productPrice)) {
            res.setCode(404);
            res.setMsg("获取产品价格失败");
        } else {
            res.setData(productPrice);
            res.setCode(200);
        }
        return res;
    }

    /**
     * 按ID获取产品
     *
     * @param productId 产品ID
     * @return {@link Res}
     */
    @GetMapping("/product/{productId}")
    public Res byIdGetProduct(@PathVariable Long productId) {
        Res res = new Res();
        Product product = productsService.byIdGetProduct(productId);
        if (Objects.equals(null, product)) {
            res.setCode(404);
            res.setMsg("获取产品失败");
        } else {
            res.setData(product);
            res.setCode(200);
        }
        return res;
    }

    @GetMapping("/addProductOrder")
    public Res addProductOrder(Long salesOrderId) {
        return saleOrderService.addProductOrder(salesOrderId);
    }

}
